package com.springboot.controller;

import com.springboot.dto.PatientListDetails;
import com.springboot.model.OrderForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorViewPatientListDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 本控制台用于医生查看在患者列表界面查看患者的预约详情
 * 点击该患者可以显示其预约详情
 * 患者列表显示的是不重复的患者列表，每个患者可以显示他的预约次数
 * 点击该患者可以查看他具体预约的几次预约详情
 */
@Controller
public class DoctorViewPatientListDetailsController {
    @Autowired
    DoctorViewPatientListDetailsService doctorViewPatientListDetailsService;
    /**
     * 医生在患者列表点击该患者查看几次预约详情
     */
    @CrossOrigin
    @RequestMapping(value = "/getPatientAppointmentInfo")
    public @ResponseBody
    String doctorViewPatientDetails(@RequestParam("doctorTel") String doctorTel,
                                    @RequestParam("patientTel") String patientTel) {
        System.out.println("医生查看患者列表的预约详情");
        try {
            UserLoginForm doctor=doctorViewPatientListDetailsService.findUserByTel(doctorTel);
            UserLoginForm patient=doctorViewPatientListDetailsService.findUserByTel(patientTel);
            int doctorId=doctor.getUserId();
            int patientId=patient.getUserId();
            System.out.println(doctorId);
            System.out.println(patientId);
            OrderForm orderForm=new OrderForm();
            orderForm.setPatientId(patientId);
            orderForm.setDoctorId(doctorId);
            System.out.println(orderForm.getBootId());
            ArrayList<OrderForm> orderForms=doctorViewPatientListDetailsService.findOrderByDoubleId(orderForm);
            System.out.println(orderForms.toString());
            int []bookId=new int[orderForms.size()];
            int []patientArray=new int[orderForms.size()];
            int bookNum=0;
            for(int i=0;i<orderForms.size();i++)
            {
                OrderForm order=orderForms.get(i);
                if(order.getProgress().equals("已就诊"))
                {
                    bookId[bookNum]=order.getBootId();
                    patientArray[bookNum]=order.getPatientId();
                    bookNum++;
                }
            }
            ArrayList<PatientListDetails> patientListDetails=new ArrayList<>();
            for(int i=0;i<bookNum;i++)
            {
                PatientListDetails patientListDetail=new PatientListDetails();
                UserLoginForm userLoginForm=doctorViewPatientListDetailsService.findUserById(patientArray[i]);
                OrderForm order=doctorViewPatientListDetailsService.findOrderByBookId(bookId[i]);
                patientListDetail.setLogoLink(userLoginForm.getLogoLink());
                patientListDetail.setPatientTel(userLoginForm.getUserTel());
                patientListDetail.setPatientName(userLoginForm.getUserName());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//日期格式
                patientListDetail.setOrderDate(format.format(order.getBootDate()));
                patientListDetail.setOrderTime(order.getBootTime());
                patientListDetails.add(patientListDetail);
            }
            //按预约日期排序
            Comparator<PatientListDetails> comparator = new Comparator<PatientListDetails>(){
                public int compare(PatientListDetails s1, PatientListDetails s2) {
                    //先按照预约日期排序
                    if(!s1.orderDate.equals(s2.orderDate)){
                        return s1.orderDate.compareTo(s2.orderDate);
                    }
                    else{
                        //预约日期相同则按照预约时间排序
                        return s1.orderTime.compareTo(s2.orderTime);
                    }
                }
            };
            //这里就会自动根据规则进行排序
            Collections.sort(patientListDetails,comparator);
            System.out.println(patientListDetails.toString());
            return patientListDetails.toString();
        } catch (Exception e) {
            return "False";
        }
    }
}
